import Vue from 'vue'
import Router from 'vue-router'
import Index from '../page/Index'
import Login from '../page/Login'
import Order from "../page/Order";
import User from "../page/User";
import Register from "../page/Register";
import Seller from "../page/Seller";

Vue.use(Router)


const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/order',
      name: 'Order',
      component: Order
    }
    , {
      path: '/user',
      name: 'User',
      component: User
    }, {
      path: '/seller',
      name: 'Seller',
      component: Seller
    }
  ]
});
// 导航守卫

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆

router.beforeEach((to, from, next) => {

  if (to.path === '/login' || to.path === '/register') {

    next();

  } else {

    let token = localStorage.getItem('token');
    // if (next('/register')){
    //   next();
    // }

    if (token === 'null' || token === '') {

      next('/login');

    } else {

      next();

    }

  }

});
export default router;
